//
//  CaseManagerTableViewController.m
//  QuickCure
//
//  Created by 007 on 16/9/12.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "CaseManagerTableViewController.h"
#import "CaseManagerTableViewCell.h"
#import "CaseTypeTableViewController.h"
#import "CaseType2TableViewController.h"
#import "CaseSectionViewCell.h"
#import "CaseThirdViewCell.h"
#import <SVProgressHUD.h>
#import <Masonry.h>
@interface CaseManagerTableViewController ()<UITextViewDelegate>
//@property (nonatomic ,strong)NSMutableArray *groups;
@property (nonatomic ,copy)NSString *detailText1;
@property (nonatomic ,copy)NSString *detailText2;
@property (nonatomic ,weak)UIButton *button;
@property (nonatomic ,weak)UITextView *textView;
//@property (nonatomic ,strong)NSMutableArray *mArray;
@property (nonatomic ,copy)NSString *text1;
@property (nonatomic ,copy)NSString *text2;
@property (nonatomic ,weak)UIImage *image;
@property (nonatomic ,copy)NSString *currentTime;
@property (nonatomic ,copy)NSString *textFieldContent;
//@property (nonnull ,strong)NSMutableArray *nsArray;
@property (nonatomic ,strong)NSDictionary *dict;
@end

@implementation CaseManagerTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonClick)];
    self.navigationItem.rightBarButtonItem = leftButton;
    
    [self setUpLeftButtonItem];
    
//    UIView *whiteView = [[UIView alloc] init];
//    [self.view addSubview:whiteView];
//    self.tableView.tableFooterView = whiteView;
}



//点击提交按钮,返回上个界面显示数据;
-(void)rightButtonClick
{
    if(self.detailText2.length ==0)
    {
        UIImage *image = [UIImage imageNamed:@"SVProgressHUD.bundle/info"];
        [self showSuccessInfoWith:image string:@"请选择具体疾病细分"];
    }
    else
    {
        NSLog(@"3333");
       NSDictionary *dict = [NSDictionary dictionary];

       NSLog(@"%@",self.detailText2);
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSString *currentTime = [formatter stringFromDate:[NSDate date]];
    self.currentTime = currentTime;

    [self.navigationController popViewControllerAnimated:NO];

        if(self.image == NULL)
        {
            
            if (self.textFieldContent == NULL)
            {
                NSDictionary *dict1 = [NSDictionary dictionary];
                dict1 = @{@"detailText1":self.detailText1
                         ,@"detailText2":self.detailText2
                         ,@"currentTime":self.currentTime};
                dict = dict1;
                self.dict = dict;
                NSLog(@"dddddddddddddd%@",dict);
                
            }else
            {
                NSDictionary *dict1 = [NSDictionary dictionary];
               dict1 = @{@"detailText1":self.detailText1
                                    ,@"detailText2":self.detailText2
                                    ,@"currentTime":self.currentTime
                        ,@"textFieldContent":self.textFieldContent
                         };

                dict = dict1;
                self.dict = dict;
            }
        }else
        {
            if (self.textFieldContent == nil)
            {

                NSDictionary *dict1 = [NSDictionary dictionary];
                dict1 = @{@"detailText1":self.detailText1
                         ,@"detailText2":self.detailText2
                         ,@"currentTime":self.currentTime
                         ,@"textFieldContent":self.textFieldContent
                         ,@"image":self.image};
                dict = dict1;
                self.dict = dict;
            }
            else
            {

                NSDictionary *dict1 = [NSDictionary dictionary];
                dict1 = @{@"detailText1":self.detailText1
                         ,@"detailText2":self.detailText2
                         ,@"currentTime":self.currentTime
                         ,@"textFieldContent":self.textFieldContent
                         ,@"image":self.image
                         ,@"textFieldContent":self.textFieldContent};

                dict = dict1;
                self.dict = dict;
            }
        
            
        }
       
       
     
        if (self.block11)
        {
            self.block11(self.dict);
        }
    }
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    else
    {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    UITableViewCell *cell  = [[UITableViewCell alloc]init];
    
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0) {
            
            cell = [CaseManagerTableViewCell cellWithTableView:tableView];
            cell.textLabel.text = @"疾病类型";
            if (self.detailText1 == nil)
            {
                
                cell.detailTextLabel.text = @"请选择";
            }else
            {
            cell.detailTextLabel.text = self.detailText1;
            }
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }else
        {
            cell = [CaseManagerTableViewCell cellWithTableView:tableView];
            cell.textLabel.text = @"疾病细分";
            if (self.detailText2 == nil)
            {
                cell.detailTextLabel.text = @"请选择";
            }else
            {
            cell.detailTextLabel.text = self.detailText2;
                //[self.mArray addObject:self.detailText2];
            }
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    }else if (indexPath.section == 1)
    {
        cell = [[CaseSectionViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        UITextView *textField = [[UITextView alloc]init];

        [cell setUserInteractionEnabled:YES];
        textField.font = [UIFont fontWithName:@"Arial" size:20.0f];

        self.textView = textField;
        self.textFieldContent= textField.text;
        
        textField.delegate = self;
      
         [textField becomeFirstResponder];
        [cell.contentView addSubview:textField];
        
        textField.backgroundColor = [UIColor colorWithWhite:0.6667 alpha:1.0];
        
        textField.layer.cornerRadius = 4;
        textField.clipsToBounds = YES;
        
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(cell.contentView).offset(35);
            make.leading.equalTo(cell).offset(15);
            make.trailing.equalTo(cell).offset(-15);
            make.height.equalTo(@150);
        }];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }else
    {
        cell = [[CaseThirdViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        UIButton *button = [[UIButton alloc]init];
        [button setBackgroundImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        self.button = button;
        [cell addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make)
         {
             make.centerY.equalTo(cell).multipliedBy(1.1);
             make.leading.equalTo(cell).offset(45);
             make.width.height.equalTo(@100);
        }];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    return cell;
}

-(void)buttonClick:(UIButton*)button
{
    UIImagePickerController *picker1 = [[UIImagePickerController alloc]init];
    picker1.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    picker1.delegate = self;
    
   // self.picker = picker1;
    picker1.allowsEditing = YES;
    [self presentViewController:picker1 animated:NO completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    
    UIImage *selectImage= info[@"UIImagePickerControllerOriginalImage"];
    [self.button setBackgroundImage:selectImage forState:UIControlStateNormal];
    self.image = selectImage;
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"选中cell");
    //tableView cellForRowAtIndexPath:<#(nonnull NSIndexPath *)#>
    if (indexPath.section ==0 )
    {
        if (indexPath.row == 0 )
        {
            CaseTypeTableViewController *caseTypeTableViewController = [[CaseTypeTableViewController alloc]init];
            caseTypeTableViewController.block= ^(NSString *detailText)
            {
                self.detailText1 = detailText;
                NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
                [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

            };
            [self.navigationController pushViewController:caseTypeTableViewController animated:NO];
        }else
        {
            
            if (self.detailText1 == nil)
            {
                UIImage *image = [UIImage imageNamed:@"SVProgressHUD.bundle/info"];
                [self showSuccessInfoWith:image string:@"先要选择疾病类型"];
        
            }else
            {
        
            CaseType2TableViewController *caseType2TableViewController = [[CaseType2TableViewController alloc]init];
            caseType2TableViewController.block2 = ^(NSString *detailText2)
            {
                self.detailText2 = detailText2;
                NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
                [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
            };
      [self.navigationController pushViewController:caseType2TableViewController animated:NO];
        }
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section{
    return 30;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 || indexPath.section == 2)
    {
        return 200;
    }
    return 50;
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    
    self.textFieldContent = textView.text;
    
}
-(void)textViewDidChange:(UITextView *)textView
{
    self.textFieldContent = textView.text;

}

#pragma mark
#pragma mark -- SVProgressHUD 设置弹窗效果
- (void)showSuccessInfoWith:(UIImage *)image string:(NSString *)str {
    
    [SVProgressHUD showImage:image status:str];
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    //设置消失时间
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // time-consuming task
        [NSThread sleepForTimeInterval:1.5];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    });
}

- (void)setUpLeftButtonItem{
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,50, 50)];
    [leftBtn setImage:[UIImage imageNamed:@"home_nav_button_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
}

- (void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
